package com.hd.trans.dialog

import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.hd.trans.R
import com.hd.trans.TranslationInterior
import com.hd.trans.camerax.CameraLightMode
import com.hd.trans.databinding.TransPopCameraLightBinding
import com.hd.trans.framework.dialog.CstBindPop
import com.hd.trans.utils.TrackerForTrans
import com.hd.trans.utils.preference.Preferences

/**
 * @author       qianjujun
 * @email        qianjujun@163.com
 * @createTime   2021/2/1 16:30
 * @describe     闪光灯设置
 */
class RCCameraLightPop(
    context: Context,
    canRotate:Boolean = false,
    private val hasAlwaysMode:Boolean = true,
    var block:(CameraLightMode)->Unit
) : CstBindPop<TransPopCameraLightBinding>(
    LayoutInflater.from(context).inflate(R.layout.trans_pop_camera_light,null),
    true,
    !canRotate
){

    private val modeItems: List<View> by lazy {
        val list = arrayListOf<View>(
            mDataBinding.btnLightAuto.apply {
                tag = CameraLightMode.AUTO
            },
            mDataBinding.btnLightOpen.apply {
                tag = CameraLightMode.OPEN
            },
            mDataBinding.btnLightClose.apply {
                tag = CameraLightMode.CLOSE
            })

        if (hasAlwaysMode) {
            list.add(mDataBinding.btnLightAlways.apply {
                tag = CameraLightMode.ALWAYS
            }
            )
        }
        else{
            mDataBinding.btnLightAlways.visibility = View.GONE
        }

        list
    }

    fun getPopHeight():Int{
        return if (hasAlwaysMode) mContext.resources.getDimensionPixelSize(R.dimen.dp_178dp) else mContext.resources.getDimensionPixelSize(R.dimen.dp_145dp)
    }

    init {
        width = if (canRotate) ViewGroup.LayoutParams.MATCH_PARENT else ViewGroup.LayoutParams.WRAP_CONTENT
        height = getPopHeight()

        val onItemClick = View.OnClickListener { v ->
            val mode = v.tag as CameraLightMode
            TranslationInterior.getTrackerListener().click(TrackerForTrans.PZFY, "闪光灯", mode.des)
            block.invoke(mode)

            displaySelectedMode(mode)

            //延迟消失，让用户看到选中的效果
            v.postDelayed({
                dismiss()
            },150)
        }

        modeItems.forEach {
            it.setOnClickListener(onItemClick)
        }

        mDataBinding.root.setOnClickListener {
            dismiss()
        }

    }

    private fun displaySelectedMode(mode:CameraLightMode){
        modeItems.forEach {
            it.isSelected = it.tag == mode
        }
    }

    fun show(view:View,mode:CameraLightMode = Preferences.flashLightMode){
        showAsDropDown(view,0,20,Gravity.RIGHT)
        displaySelectedMode(mode)
    }

    fun showTop(view:View,mode:CameraLightMode = Preferences.flashLightMode,isLandScape:Boolean = false){
        showAsDropDown(view,0,-getPopHeight()-view.measuredHeight-mContext.resources.getDimensionPixelSize(R.dimen.dp_10dp))
        displaySelectedMode(mode)
        mDataBinding.llMain.post {
            if (isLandScape){
                mDataBinding.llMain.rotation = 90f
                mDataBinding.llMain.translationY = mDataBinding.llMain.measuredHeight.toFloat()/2 - mDataBinding.llMain.measuredWidth.toFloat()/2
            }
            else{
                mDataBinding.llMain.rotation = 0f
                mDataBinding.llMain.translationY = 0f
            }
        }
    }

}